package com.cspg.design;

import android.app.Application;

import androidx.multidex.MultiDex;

import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.cspg.design.adapter.DefaultLoadingAdapter;
import com.cspg.design.bean.UserInfo;
import com.cspg.design.bean.userInfoBean.UserInfoBean;
import com.cspg.design.dao.gen.DaoMaster;
import com.cspg.design.dao.gen.DaoSession;
import com.cspg.design.dao.gen.UserInfoBeanDao;
import com.cspg.design.request.DeviceLibrary;
import com.cspg.design.utils.AppManager;
import com.cspg.design.widget.Loading;
import com.facebook.stetho.Stetho;
import com.facebook.stetho.okhttp3.StethoInterceptor;

import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.rx.RxDao;
import org.greenrobot.greendao.rx.RxQuery;

import java.util.List;

import okhttp3.OkHttpClient;

public class CSPGApplication extends Application {
    private DaoSession daoSession;
    private RxDao<UserInfoBean, Long> userInfoBeanLongRxDao;
    private RxQuery<UserInfoBean> userInfoBeanRxQuery;
    private DeviceLibrary deviceLibrary;
    @Override
    public void onCreate() {
        super.onCreate();
        // 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
        // 默认本地个性化地图初始化方法
        SDKInitializer.initialize(getApplicationContext());
        // 自4.3.0起，百度地图SDK所有接口均支持百度坐标和国测局坐标，用此方法设置您使用的坐标类型.
        // 包括BD09LL和GCJ02两种坐标，默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.BD09LL);
        // regular SQLite database
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "cspg-ei");
        Database db = helper.getWritableDb();
        AppManager.getAppManager();
//        DaoMaster.dropAllTables(db,true);
//        DaoMaster.createAllTables(db,true);
        // encrypted SQLCipher databases
        // note: you need to add SQLCipher to your dependencies, check the build.gradle file
        // DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db-encrypted");
        // Database db = helper.getEncryptedWritableDb("encryption-key");
        MultiDex.install(this);

        Loading.getIns().setAdapter(new DefaultLoadingAdapter(this));

        daoSession = new DaoMaster(db).newSession();
        deviceLibrary = new DeviceLibrary(daoSession);

        List<UserInfoBean> userInfoBeans = daoSession.getUserInfoBeanDao().queryBuilder()
                .orderAsc(UserInfoBeanDao.Properties.Date).list();
        if (userInfoBeans.size() > 0)
        {
            UserInfoBean userInfoBean = userInfoBeans.get(0);
            String token = userInfoBean.getToken();
            System.out.println("token:"+token);
            UserInfo userInfo = UserInfo.getInstance();
            userInfo.userName = userInfoBean.getUserName();
            userInfo.password = userInfoBean.getPassword();
            userInfo.token = userInfoBean.getToken();
            userInfo.role = userInfoBean.getRole();
            userInfo.unit = userInfoBean.getUnit();
            UserInfo.setUserInfo(userInfo);
            requestServerDeviceModelData();
        }

        Stetho.initializeWithDefaults(this);
        new OkHttpClient.Builder()
                .addNetworkInterceptor(new StethoInterceptor())
                .build();
    }
    public DaoSession getDaoSession() {
        return daoSession;
    }


    private void requestServerDeviceModelData()
    {
        deviceLibrary.requestPoleLibrary();
        deviceLibrary.requestTransformerLibrary();
        deviceLibrary.requestWireLibrary();
    }


}
